/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.ComponentModel.Design;

namespace DaveSexton.DocProject
{
	internal sealed class ComponentChangeService : MarshalByRefObject, IComponentChangeService
	{
		public static readonly ComponentChangeService Instance = new ComponentChangeService();

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="ComponentChangeService" /> class.
		/// </summary>
		private ComponentChangeService()
		{
		}
		#endregion

		#region Methods
		[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.LinkDemand,
			Flags = System.Security.Permissions.SecurityPermissionFlag.Infrastructure)]
		public override object InitializeLifetimeService()
		{
			return null;
		}
		#endregion

		#region Events
		private readonly object ComponentAddedEventLock = new object();
		private ComponentEventHandler ComponentAddedEvent;
		private readonly object ComponentAddingEventLock = new object();
		private ComponentEventHandler ComponentAddingEvent;
		private readonly object ComponentChangedEventLock = new object();
		private ComponentChangedEventHandler ComponentChangedEvent;
		private readonly object ComponentChangingEventLock = new object();
		private ComponentChangingEventHandler ComponentChangingEvent;
		private readonly object ComponentRemovedEventLock = new object();
		private ComponentEventHandler ComponentRemovedEvent;
		private readonly object ComponentRemovingEventLock = new object();
		private ComponentEventHandler ComponentRemovingEvent;
		private readonly object ComponentRenameEventLock = new object();
		private ComponentRenameEventHandler ComponentRenameEvent;

		public event ComponentRenameEventHandler ComponentRename
		{
			add
			{
				lock (ComponentRenameEventLock)
				{
					ComponentRenameEvent += value;
				}
			}
			remove
			{
				lock (ComponentRenameEventLock)
				{
					ComponentRenameEvent -= value;
				}
			}
		}

		/// <summary>
		/// Raises the <see cref="ComponentRename" /> event.
		/// </summary>
		/// <param name="e"><see cref="ComponentEventArgs" /> object that provides the arguments for the event.</param>
		/*private void OnComponentRename(ComponentRenameEventArgs e)
		{
			ComponentRenameEventHandler handler = null;

			lock (ComponentRenameEventLock)
			{
				handler = ComponentRenameEvent;
			}

			if (handler != null)
				handler(this, e);
		}*/

		public event ComponentEventHandler ComponentRemoving
		{
			add
			{
				lock (ComponentRemovingEventLock)
				{
					ComponentRemovingEvent += value;
				}
			}
			remove
			{
				lock (ComponentRemovingEventLock)
				{
					ComponentRemovingEvent -= value;
				}
			}
		}

		/// <summary>
		/// Raises the <see cref="ComponentRemoving" /> event.
		/// </summary>
		/// <param name="e"><see cref="ComponentEventArgs" /> object that provides the arguments for the event.</param>
		/*private void OnComponentRemoving(ComponentEventArgs e)
		{
			ComponentEventHandler handler = null;

			lock (ComponentRemovingEventLock)
			{
				handler = ComponentRemovingEvent;
			}

			if (handler != null)
				handler(this, e);
		}*/

		public event ComponentEventHandler ComponentRemoved
		{
			add
			{
				lock (ComponentRemovedEventLock)
				{
					ComponentRemovedEvent += value;
				}
			}
			remove
			{
				lock (ComponentRemovedEventLock)
				{
					ComponentRemovedEvent -= value;
				}
			}
		}

		/// <summary>
		/// Raises the <see cref="ComponentRemoved" /> event.
		/// </summary>
		/// <param name="e"><see cref="ComponentEventArgs" /> object that provides the arguments for the event.</param>
		/*private void OnComponentRemoved(ComponentEventArgs e)
		{
			ComponentEventHandler handler = null;

			lock (ComponentRemovedEventLock)
			{
				handler = ComponentRemovedEvent;
			}

			if (handler != null)
				handler(this, e);
		}*/

		public event ComponentChangingEventHandler ComponentChanging
		{
			add
			{
				lock (ComponentChangingEventLock)
				{
					ComponentChangingEvent += value;
				}
			}
			remove
			{
				lock (ComponentChangingEventLock)
				{
					ComponentChangingEvent -= value;
				}
			}
		}

		/// <summary>
		/// Raises the <see cref="ComponentChanging" /> event.
		/// </summary>
		/// <param name="e"><see cref="ComponentChangedEventArgs" /> object that provides the arguments for the event.</param>
		private void OnComponentChanging(ComponentChangingEventArgs e)
		{
			ComponentChangingEventHandler handler = null;

			lock (ComponentChangingEventLock)
			{
				handler = ComponentChangingEvent;
			}

			if (handler != null)
				handler(this, e);
		}

		public event ComponentChangedEventHandler ComponentChanged
		{
			add
			{
				lock (ComponentChangedEventLock)
				{
					ComponentChangedEvent += value;
				}
			}
			remove
			{
				lock (ComponentChangedEventLock)
				{
					ComponentChangedEvent -= value;
				}
			}
		}

		/// <summary>
		/// Raises the <see cref="ComponentChanged" /> event.
		/// </summary>
		/// <param name="e"><see cref="ComponentChangedEventArgs" /> object that provides the arguments for the event.</param>
		private void OnComponentChanged(ComponentChangedEventArgs e)
		{
			ComponentChangedEventHandler handler = null;

			lock (ComponentChangedEventLock)
			{
				handler = ComponentChangedEvent;
			}

			if (handler != null)
				handler(this, e);
		}

		public event ComponentEventHandler ComponentAdding
		{
			add
			{
				lock (ComponentAddingEventLock)
				{
					ComponentAddingEvent += value;
				}
			}
			remove
			{
				lock (ComponentAddingEventLock)
				{
					ComponentAddingEvent -= value;
				}
			}
		}

		/// <summary>
		/// Raises the <see cref="ComponentAdding" /> event.
		/// </summary>
		/// <param name="e"><see cref="ComponentEventArgs" /> object that provides the arguments for the event.</param>
		/*private void OnComponentAdding(ComponentEventArgs e)
		{
			ComponentEventHandler handler = null;

			lock (ComponentAddingEventLock)
			{
				handler = ComponentAddingEvent;
			}

			if (handler != null)
				handler(this, e);
		}*/

		public event ComponentEventHandler ComponentAdded
		{
			add
			{
				lock (ComponentAddedEventLock)
				{
					ComponentAddedEvent += value;
				}
			}
			remove
			{
				lock (ComponentAddedEventLock)
				{
					ComponentAddedEvent -= value;
				}
			}
		}

		/// <summary>
		/// Raises the <see cref="ComponentAdded" /> event.
		/// </summary>
		/// <param name="e"><see cref="ComponentEventArgs" /> object that provides the arguments for the event.</param>
		/*private void OnComponentAdded(ComponentEventArgs e)
		{
			ComponentEventHandler handler = null;

			lock (ComponentAddedEventLock)
			{
				handler = ComponentAddedEvent;
			}

			if (handler != null)
				handler(this, e);
		}*/

		public void OnComponentChanged(object component, MemberDescriptor member, object oldValue, object newValue)
		{
			OnComponentChanged(new ComponentChangedEventArgs(component, member, oldValue, newValue));
		}

		public void OnComponentChanging(object component, MemberDescriptor member)
		{
			OnComponentChanging(new ComponentChangingEventArgs(component, member));
		}
		#endregion
	}
}
